﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tracker.Services;
using Tracker.Data.DataTransfer;
using Tracker.Helper;
using Tracker.Views;


namespace Tracker
{
    public partial class Options : Form,IOptionView
    {
        private ITaskData td;
        private AppSetting config;

        public Options(ITaskData t, AppSetting c)
        {
            InitializeComponent();
            
            td = t;
            config = c;
          
        }

        #region event handling 
        private void buttonShowFolderDialog_Click(object sender, EventArgs e)
        {
            DialogResult r = folderBrowserDialogRootPath.ShowDialog();
            if (r == DialogResult.OK)
            {
                textBoxRootPath.Text = folderBrowserDialogRootPath.SelectedPath;
                ChangeRootPathEvent(sender, e);
            }
        }


        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void Options_Load(object sender, EventArgs e)
        {
            RefreshRootPathEvent(sender, e);
            RefreshTaskFieldsEvent(sender, e);
            RefreshStatusDomainEvent(sender, e);


        }

        private void buttonDeleteField_Click(object sender, EventArgs e)
        {
            var oa = dataGridViewTaskFields.CurrentRow.DataBoundItem as ObjectAttribute;
            DeleteTaskFieldDomainEvent(sender, oa);
            RefreshTaskFieldsEvent(sender, e);
        }


        private void dataGridViewTaskFields_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            td.UpdateTaskFieldDomain((dataGridViewTaskFields.Rows[e.RowIndex].DataBoundItem as ObjectAttribute));

        }

        private void buttonDeleteStatusDomain_Click(object sender, EventArgs e)
        {
            DeleteStatusDomainEvent(sender, dataGridViewStatusDomain.CurrentRow.DataBoundItem as StatusDomain);
            RefreshStatusDomainEvent(sender, e);
        }

        private void dataGridViewStatusDomain_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            UpdateStatusDomainEvent(sender, (dataGridViewStatusDomain.Rows[e.RowIndex].DataBoundItem as StatusDomain));

        }


        #endregion

        #region interface implementation

        public BindingList<ObjectAttribute> TaskFieldView
        {
            get
            {
                return dataGridViewTaskFields.DataSource as BindingList<ObjectAttribute>;
            }
            set
            {
                dataGridViewTaskFields.ColumnHeadersVisible = false;
                dataGridViewTaskFields.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

                dataGridViewTaskFields.DataSource = value;
                foreach (DataGridViewColumn item in dataGridViewTaskFields.Columns)
                {
                    if (item.Name != "AttributeName")
                    {
                        item.Visible = false;
                    }
                }

                dataGridViewTaskFields.Refresh();
            }
        }

        public BindingList<StatusDomain> StatusDomainView
        {
            get
            {
                 return dataGridViewStatusDomain.DataSource as  BindingList<StatusDomain>;
            }
            set
            {
                dataGridViewStatusDomain.ColumnHeadersVisible = false;
                dataGridViewStatusDomain.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

                dataGridViewStatusDomain.DataSource = value;
                foreach (DataGridViewColumn item in dataGridViewStatusDomain.Columns)
                {
                    if (item.Name != "Status")
                    {
                        item.Visible = false;
                    }
                }

                dataGridViewStatusDomain.Refresh();
            }
        }

        public string RootPath
        {
            get
            {
                return textBoxRootPath.Text;
            }
            set
            {
                textBoxRootPath.Text = value;
            }
        }

        public event EventHandler RefreshTaskFieldsEvent;
        public event EventHandler<ObjectAttribute> DeleteTaskFieldDomainEvent;
        public event EventHandler RefreshRootPathEvent;
        public event EventHandler ChangeRootPathEvent;
        public event EventHandler RefreshStatusDomainEvent;
        public event EventHandler<StatusDomain> DeleteStatusDomainEvent;
        public event EventHandler<StatusDomain> UpdateStatusDomainEvent;  
        
        #endregion




        
        
    }
}
